﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using Itasa.API;

namespace Itasa.RSS
{
    static internal class RSS_API
    {

        static internal List<RSS_Sub> parseRSS(String url)
        {
            WebClient Client = new WebClient();
            String xml = Client.DownloadString(url);
            XmlDocument xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            XmlNode xn = xd.DocumentElement;
            List<RSS_Sub> lrs = new List<RSS_Sub>();
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Sub rs = new RSS_Sub(xnc);
                        rs.SubInfo = Itasa.API.ITASA_API.subInfo(rs.ID.ToString());
                        lrs.Add(rs);
                    }
                }
            }
            return lrs;
        }

        static internal List<RSS_Torrent> eztvRSS(Show show, String version)
        {
            if (version == "Normale") version = "";
            WebClient Client = new WebClient();
            String xml = Client.DownloadString("http://www.ezrss.it/search/index.php?show_name=" + show.Name + "&show_name_exact=true&date=&quality=" + version + "&release_group=&mode=rss");
            XmlDocument xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            XmlNode xn = xd.DocumentElement;
            List<RSS_Torrent> lrs = new List<RSS_Torrent>();
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.EZTV);
                        lrs.Add(rs);
                    }
                }
            }
            return lrs;
        }
        //http://www.ezrss.it/search/index.php?show_name=dexter&date=&quality=720p&release_group=&episode_title=&season=4&episode=3&video_format=&audio_format=&modifier=&mode=advanced

        static internal List<RSS_Torrent> isohuntRSS(Show show, String version)
        {
            String url;
            if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "?iht=3&noSL";
            else url = "http://isohunt.com/js/rss/" + show.Name + "+" + version + "?iht=3&noSL";
            WebClient Client = new WebClient();
            String xml = Client.DownloadString(url);
            XmlDocument xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            XmlNode xn = xd.DocumentElement;
            List<RSS_Torrent> lrs = new List<RSS_Torrent>();
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                        lrs.Add(rs);
                    }
                }
            }
            if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "?iht=1&noSL";
            else url = "http://isohunt.com/js/rss/" + show.Name + "+" + version + "?iht=1&noSL";
            xml = Client.DownloadString(url);
            xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            xn = xd.DocumentElement;
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                        lrs.Add(rs);
                    }
                }
            }
            if(show.Genres.Contains("Anime"))
            {
                if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "?iht=7&noSL";
                else url = "http://isohunt.com/js/rss/" + show.Name + "+" + version + "?iht=7&noSL";
                xml = Client.DownloadString(url);
                xd = new System.Xml.XmlDocument();
                xd.LoadXml(xml);
                xn = xd.DocumentElement;
                if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
                {
                    xn = xn.FirstChild;
                    foreach (XmlNode xnc in xn.ChildNodes)
                    {
                        if (xnc.Name == "item")
                        {
                            RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                            lrs.Add(rs);
                        }
                    }
                }
            }
            return lrs;
        }

        static internal List<RSS_Torrent> isohuntRSSLatest(Show show, String version)
        {
            String url;
            if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "?iht=3&noSL";
            else url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "+" + version + "?iht=3&noSL";
            WebClient Client = new WebClient();
            String xml = Client.DownloadString(url);
            XmlDocument xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            XmlNode xn = xd.DocumentElement;
            List<RSS_Torrent> lrs = new List<RSS_Torrent>();
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                        lrs.Add(rs);
                    }
                }
            }
            if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "?iht=1&noSL";
            else url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "+" + version + "?iht=1&noSL";
            xml = Client.DownloadString(url);
            xd = new System.Xml.XmlDocument();
            xd.LoadXml(xml);
            xn = xd.DocumentElement;
            if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
            {
                xn = xn.FirstChild;
                foreach (XmlNode xnc in xn.ChildNodes)
                {
                    if (xnc.Name == "item")
                    {
                        RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                        lrs.Add(rs);
                    }
                }
            }
            if (show.Genres.Contains("Anime"))
            {
                if (version == "Normale") url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "?iht=7&noSL";
                else url = "http://isohunt.com/js/rss/" + show.Name + "+" + show.ToLatestString() + "+" + version + "?iht=7&noSL";
                xml = Client.DownloadString(url);
                xd = new System.Xml.XmlDocument();
                xd.LoadXml(xml);
                xn = xd.DocumentElement;
                if (xn.Name == "rss" && xn.FirstChild.Name == "channel")
                {
                    xn = xn.FirstChild;
                    foreach (XmlNode xnc in xn.ChildNodes)
                    {
                        if (xnc.Name == "item")
                        {
                            RSS_Torrent rs = new RSS_Torrent(xnc, RSS_Torrent.Source.ISOHUNT);
                            lrs.Add(rs);
                        }
                    }
                }
            }
            return lrs;
        }

    }
}
